package com.zth.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.zth.pojo.Comment;
import com.zth.service.CommentService;

@RestController
@RequestMapping("comment")
public class CommentController {
	@Autowired
	private CommentService commentService;

	@RequestMapping("commentAll")
	public Map<String, Object> commentAll(@RequestParam(defaultValue = "1", name = "page") int page,
			@RequestParam(defaultValue = "10", name = "limit") int pageSize,Comment comment) {
		List<Comment> list = commentService.commentAll(page, pageSize,comment);
		PageInfo<Comment> info = new PageInfo<>(list);

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("code", 0);
		map.put("msg", "");
		map.put("count", info.getTotal());
		map.put("data", info.getList());

		return map;
	}

	@RequestMapping("/add")
	int add(Comment comment) {
		if(comment.getWebsite_url()==null || comment.getWebsite_url().equals("")) {
			comment.setWebsite_url("www.zth.com");
		}
		return commentService.add(comment);
	}

	@RequestMapping("findById")
	public Map<String, Object> findById(int id) {
		List<Comment> list = commentService.findById(id);
		System.out.println();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("length", list.size());
		map.put("data", list);
		return map;
	}

	@RequestMapping("status")
	public int status(@RequestParam(name = "ids[]") int[] ids) {
		return commentService.status(ids);
	}

	@RequestMapping("deleteBatch")
	public int deleteBatch(@RequestParam(name = "ids[]") int[] ids) {
		return commentService.deleteBatch(ids);
	}

	@RequestMapping("reply")
	public int reply(Comment comment) {
		System.out.println(commentService.reply(comment));
		return commentService.reply(comment);
	}
}
